package com.yxc.plan.wolf.service.impl;

import com.yxc.plan.wolf.common.ServerResponse;
import com.yxc.plan.wolf.domain.Comment;
import com.yxc.plan.wolf.domain.Tweet;
import com.yxc.plan.wolf.domain.User;
import com.yxc.plan.wolf.domain.po.AddTweetPO;
import com.yxc.plan.wolf.domain.repository.TweetRepository;
import com.yxc.plan.wolf.domain.repository.UserRepository;
import com.yxc.plan.wolf.service.interfaces.TweetService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
* @Description : 
* @Author      : abc@vanke.com
* @Date        : 2018-12-17 19:27
*/
@Service
public class TweetServiceImpl implements TweetService {

    @Autowired
    private TweetRepository tweetRepository;

    @Autowired
    private UserRepository userRepository;

    @Override
    public ServerResponse add(AddTweetPO addTweetPO) {
        Tweet tweet = new Tweet();
        BeanUtils.copyProperties(addTweetPO, tweet);
        tweet = tweetRepository.save(tweet);
        if (tweet.getId() > 0) {
            return ServerResponse.createBySuccessMessage("发布成功");
        } else {
            return ServerResponse.createByErrorMessage("发布失败");
        }
    }

    @Override
    public ServerResponse<List<Tweet>> list(Long projectId, Pageable pageable) {
        Page<Tweet> tweetPage = tweetRepository.findByDeletedAndProjectId(false,projectId, pageable);
        List<Tweet> tweetList = tweetPage.getContent();
        List<Long> userIds = new ArrayList<>();
        for (Tweet tweet: tweetList) {
            userIds.add(tweet.getUserId());
        }

        List<User> userList = userRepository.findByDeletedAndIdIn(false, userIds);
        HashMap<Long, User> userHashMap = new HashMap<>();
        for (User user: userList) {
            userHashMap.put(user.getId(), user);
        }

        for (Tweet tweet: tweetList) {
            User user = userHashMap.get(tweet.getUserId());
            tweet.setUser(user);
        }

        return ServerResponse.createBySuccess("访问成功", tweetList);
    }

    @Override
    public ServerResponse<List<Tweet>> search(Long projectId, String key) {
        List<Tweet> tweetList = tweetRepository.findByDeletedAndProjectIdAndDetailContaining(false, projectId, key);
        List<Long> userIds = new ArrayList<>();
        for (Tweet tweet: tweetList) {
            userIds.add(tweet.getUserId());
        }

        List<User> userList = userRepository.findByDeletedAndIdIn(false, userIds);
        HashMap<Long, User> userHashMap = new HashMap<>();
        for (User user: userList) {
            userHashMap.put(user.getId(), user);
        }

        for (Tweet tweet: tweetList) {
            User user = userHashMap.get(tweet.getUserId());
            tweet.setUser(user);
        }

        return ServerResponse.createBySuccess("访问成功", tweetList);
    }

    @Override
    public ServerResponse<List<Tweet>> noComment(Long projectId, Pageable pageable) {
        Page<Tweet> tweetPage = tweetRepository.findByDeletedAndProjectIdAndCommentCount(false, projectId, new Long(0), pageable);
        List<Tweet> tweetList = tweetPage.getContent();
        List<Long> userIds = new ArrayList<>();
        for (Tweet tweet: tweetList) {
            userIds.add(tweet.getUserId());
        }

        List<User> userList = userRepository.findByDeletedAndIdIn(false, userIds);
        HashMap<Long, User> userHashMap = new HashMap<>();
        for (User user: userList) {
            userHashMap.put(user.getId(), user);
        }

        for (Tweet tweet: tweetList) {
            User user = userHashMap.get(tweet.getUserId());
            tweet.setUser(user);
        }

        return ServerResponse.createBySuccess("访问成功", tweetList);
    }
}
